#include "MemPool.h"

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <athread.h>
//#include "utils/acelog.h"
extern struct MemPool pool;
#ifdef ACEMESH_SCHEDULER_PROFILING
extern unsigned long mempool_alloc_block_num;
extern int my_mpi_rank;
#endif
struct block *slave_mempool_allocblock(struct MemPool pool)
{
#ifdef TARG_SW5
    struct block *myblock = (struct block *)malloc(sizeof(struct block));
    myblock->data = malloc(UNITSIZE);
#elif TARG_SW9
    struct block* myblock=(struct block*)libc_uncached_aligned_malloc(sizeof(struct block));
    myblock->data=(void *)libc_uncached_aligned_malloc(UNITSIZE);
// #ifdef ACEMESH_SCHEDULER_PROFILING
//     if(my_mpi_rank<=0)
//         printf("my_block_addr=%p, myblock_data_addr=%p\n",myblock,myblock->data);
// #endif
#endif
#ifdef ACEMESH_SCHEDULER_PROFILING
    mempool_alloc_block_num++;
#endif

    myblock->next = NULL;
    return myblock;
}

void *slave_acemesh_myalloc_aligned_64(int datasize)
{
    if ((long long)pool.self1 % 64 > 0)
        pool.self1 = (void *)((char *)pool.self1 + (64 - (long long)pool.self1 % 64));
       printf("myalloc_aligned_64\n");
   if (((char *)pool.EndBlock - (char *)pool.self1) < datasize) {
        /*
        if (pool.FreeMemBlock == NULL) {
            pool.FreeMemBlock = slave_mempool_allocblock(pool);
        }
        struct block *pCurUnit = pool.FreeMemBlock;
        pool.FreeMemBlock = pool.FreeMemBlock->next;
        pCurUnit->next = pool.AllocatedMemBlock;
        pool.AllocatedMemBlock = pCurUnit;
        pool.EndBlock = (void *)((char *)(pCurUnit->data) + UNITSIZE);
        pool.self1 = (void *)(pCurUnit->data);
    */
        printf("need_remalloc_memmery\n");
    }
    
    void *self = (void *)pool.self1;
    pool.self1 = (void *)((char *)pool.self1 + datasize);
    return (void *)self;
}
void *slave_acemesh_myalloc_aligned_16(int datasize)
{
    if ((long long)pool.self1 % 16 > 0)
        pool.self1 = (void *)((char *)pool.self1 + (16 - (long long)pool.self1 % 16));
       printf("myalloc_aligned_16\n");
   if (((char *)pool.EndBlock - (char *)pool.self1) < datasize) {
        /*
 *         if (pool.FreeMemBlock == NULL) {
 *                     pool.FreeMemBlock = slave_mempool_allocblock(pool);
 *                             }
 *                                     struct block *pCurUnit = pool.FreeMemBlock;
 *                                             pool.FreeMemBlock = pool.FreeMemBlock->next;
 *                                                     pCurUnit->next = pool.AllocatedMemBlock;
 *                                                             pool.AllocatedMemBlock = pCurUnit;
 *                                                                     pool.EndBlock = (void *)((char *)(pCurUnit->data) + UNITSIZE);
 *                                                                             pool.self1 = (void *)(pCurUnit->data);
 *                                                                                 */
        printf("need_remalloc_memmery\n");
    }
    
    void *self = (void *)pool.self1;
    pool.self1 = (void *)((char *)pool.self1 + datasize);
    return (void *)self;
}
